package com.nhom4.ADP;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * @version 1.1.1 26 May 2014
 * @author Team Four
 * @review Leader
 */

import javax.swing.*;

public class Gui implements ActionListener, KeyListener {
	private int operator;
	public int x1, y1, x2, y2;
	public JFrame f = new JFrame("Fraction GUI");
	public Label l1 = new Label("Fraction 1");
	public Label l2 = new Label("Numerator");
	public Label l3 = new Label("Denominator");
	public Label l4 = new Label("Fraction 2");
	public Label l5 = new Label("Numerator");
	public Label l6 = new Label("Denominator");
	public Label l7 = new Label("Choose the openrator");
	public Label l8 = new Label("Fraction 3");
	public Label l9 = new Label("Numerator");
	public Label l10 = new Label("Denominator");

	public JComboBox c = new JComboBox();

	public JRadioButton r3 = new JRadioButton("Square");
	public Button b1 = new Button("Solve");
	public Button b2 = new Button("Reset");
	public Button b3 = new Button("Exit");
	public Font font = new Font("Tahoma", Font.BOLD, 16);
	public Font font1 = new Font("Tahoma", Font.BOLD, 14);
	public JTextField t1 = new JTextField(20);
	public JTextField t2 = new JTextField(20);
	public JTextField t3 = new JTextField(20);
	public JTextField t4 = new JTextField(20);
	public JTextField t5 = new JTextField(20);
	public JTextField t6 = new JTextField(20);

	public Gui() {
		f.setSize(400, 430);
		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
		f.setLocationRelativeTo(null);
		f.setLayout(null);

		l1.setBounds(10, 20, 100, 20);
		l1.setFont(font);
		f.add(l1);

		l2.setBounds(30, 50, 100, 20);
		l2.setFont(font1);
		f.add(l2);

		l3.setBounds(30, 80, 100, 20);
		l3.setFont(font1);
		f.add(l3);

		l4.setBounds(10, 110, 100, 20);
		l4.setFont(font);
		f.add(l4);

		l5.setBounds(30, 140, 100, 20);
		l5.setFont(font1);
		f.add(l5);

		l6.setBounds(30, 170, 100, 20);
		l6.setFont(font1);
		f.add(l6);

		l7.setBounds(10, 210, 180, 20);
		l7.setFont(font);
		f.add(l7);

		l8.setBounds(10, 250, 100, 20);
		l8.setFont(font);
		f.add(l8);

		l9.setBounds(30, 280, 100, 20);
		l9.setFont(font1);
		f.add(l9);

		l10.setBounds(30, 310, 100, 20);
		l10.setFont(font1);
		f.add(l10);

		t1.setBounds(200, 45, 180, 25);
		f.add(t1);

		t2.setBounds(200, 75, 180, 25);
		f.add(t2);

		t3.setBounds(200, 135, 180, 25);
		f.add(t3);

		t4.setBounds(200, 165, 180, 25);
		f.add(t4);

		t5.setBounds(200, 275, 180, 25);
		t5.setBackground(Color.BLACK);
		t5.setEnabled(false);
		f.add(t5);

		t6.setBounds(200, 305, 180, 25);
		t6.setBackground(Color.BLACK);
		t6.setEnabled(false);
		f.add(t6);

		b1.setBounds(10, 340, 120, 40);
		b1.setBackground(Color.yellow);
		b1.setFont(font);
		f.add(b1);

		b2.setBounds(130, 340, 120, 40);
		b2.setBackground(Color.yellow);
		b2.setFont(font);
		f.add(b2);

		b3.setBounds(250, 340, 120, 40);
		b3.setBackground(Color.yellow);
		b3.setFont(font);
		f.add(b3);

		c.setBounds(200, 200, 180, 40);
		String h[] = { "+", "-", "*", "/" };
		for (int i = 0; i < 4; i++)
			c.addItem(h[i]);

		f.add(c);
		c.addActionListener(this);
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);

		t1.addKeyListener(this);
		t2.addKeyListener(this);
		t3.addKeyListener(this);
		t4.addKeyListener(this);
		c.addKeyListener(this);
		b1.addKeyListener(this);
		b2.addKeyListener(this);
		b3.addKeyListener(this);

		f.setVisible(true);
	}

	public static void main(String agrs[]) {
		Gui p = new Gui();
	}

	public void reset() {
		int num = c.getSelectedIndex();
		for (int i = 0; i < 4; i++)
			if (num == i)
				operator = i;
	}

	public boolean check(String z) {
		try {
			Integer.parseInt(z);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public void solve() {
		reset();
		int x1 = Integer.parseInt(t1.getText());
		int y1 = Integer.parseInt(t2.getText());
		Fraction f1 = new Fraction(x1, y1);
		int x2 = Integer.parseInt(t3.getText());
		int y2 = Integer.parseInt(t4.getText());
		Fraction f2 = new Fraction(x2, y2);

		Processor x = new Processor();
		if (operator == 0)
			x.tong(f1, f2);
		if (operator == 1)
			x.hieu(f1, f2);
		if (operator == 2)
			x.tich(f1, f2);
		if (operator == 3)
			x.thuong(f1, f2);
		t5.setText(x.z1);
		t6.setText(x.z2);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b2) {
			t1.setText("");
			t2.setText("");
			t3.setText("");
			t4.setText("");
			t5.setText("");
			t6.setText("");
			t1.requestFocus();
		}
		if (e.getSource() == b3) {
			System.exit(0);
		}
		if (e.getSource() == b1) {
			int u = 0;

			if (check(t1.getText()) == false)
				u++;
			if (check(t2.getText()) == false)
				u++;
			if (check(t3.getText()) == false)
				u++;
			if (check(t4.getText()) == false)
				u++;

			if (u == 0)
				solve();
		}
	}

	public void keyPressed(KeyEvent e) {
		if (e.getSource() == t1) {
			if (e.getKeyCode() == e.VK_ENTER || e.getKeyCode() == e.VK_TAB
					|| e.getKeyCode() == e.VK_DOWN) {
				String h = t1.getText();
				if (h.length() == 0 || check(h) == false)
					JOptionPane
							.showMessageDialog(
									null,
									"Báº¡n Ä‘Ã£ nháº­p sai hoáº·c chÆ°a nháº­p giÃ¡ trá»‹\nMá»�i báº¡n nháº­p láº¡i");
				else
					t1.transferFocus();
			}
		}
		if (e.getSource() == t2) {
			if (e.getKeyCode() == e.VK_ENTER || e.getKeyCode() == e.VK_TAB
					|| e.getKeyCode() == e.VK_DOWN) {
				String h = t2.getText();
				if (h.length() == 0 || check(h) == false)
					JOptionPane
							.showMessageDialog(
									null,
									"Báº¡n Ä‘Ã£ nháº­p sai hoáº·c chÆ°a nháº­p giÃ¡ trá»‹\nMá»�i báº¡n nháº­p láº¡i");
				else
					t2.transferFocus();
			}
			if (e.getKeyCode() == e.VK_UP)
				t1.requestFocus();
		}
		if (e.getSource() == t3) {
			if (e.getKeyCode() == e.VK_ENTER || e.getKeyCode() == e.VK_TAB
					|| e.getKeyCode() == e.VK_DOWN) {
				String h = t3.getText();
				if (h.length() == 0 || check(h) == false)
					JOptionPane
							.showMessageDialog(
									null,
									"Báº¡n Ä‘Ã£ nháº­p sai hoáº·c chÆ°a nháº­p giÃ¡ trá»‹\nMá»�i báº¡n nháº­p láº¡i");
				else
					t3.transferFocus();
			}
			if (e.getKeyCode() == e.VK_UP)
				t2.requestFocus();
		}
		if (e.getSource() == t4) {
			if (e.getKeyCode() == e.VK_ENTER || e.getKeyCode() == e.VK_TAB
					|| e.getKeyCode() == e.VK_DOWN || e.getKeyCode() == e.VK_UP) {
				String h = t4.getText();
				if (h.length() == 0 || check(h) == false)
					JOptionPane
							.showMessageDialog(
									null,
									"Báº¡n Ä‘Ã£ nháº­p sai hoáº·c chÆ°a nháº­p giÃ¡ trá»‹\nMá»�i báº¡n nháº­p láº¡i");
				else
					t4.transferFocus();
			}
			if (e.getKeyCode() == e.VK_UP)
				t3.requestFocus();
		}
		if (e.getSource() == c) {
			if (e.getKeyCode() == e.VK_TAB || e.getKeyCode() == e.VK_ENTER)
				b1.requestFocus();

		}
		if (e.getSource() == b1) {
			if (e.getKeyCode() == e.VK_ENTER) {
				int u = 0;
				if (check(t1.getText()) == false)
					u++;
				if (check(t2.getText()) == false)
					u++;
				if (check(t3.getText()) == false)
					u++;
				if (check(t4.getText()) == false)
					u++;
				if (u == 0)
					solve();
			}
			if (e.getKeyCode() == e.VK_UP)
				c.requestFocus();
			if (e.getKeyCode() == e.VK_RIGHT)
				b2.requestFocus();
			if (e.getKeyCode() == e.VK_SPACE)
				solve();
		}
		if (e.getSource() == b2) {
			if (e.getKeyCode() == e.VK_ENTER) {
				t1.setText("");
				t2.setText("");
				t3.setText("");
				t4.setText("");
				t5.setText("");
				t6.setText("");
				t1.requestFocus();
			}
			if (e.getKeyCode() == e.VK_UP)
				c.requestFocus();
			if (e.getKeyCode() == e.VK_RIGHT)
				b3.requestFocus();
			if (e.getKeyCode() == e.VK_LEFT)
				b1.requestFocus();
			if (e.getKeyCode() == e.VK_SPACE) {
				t1.setText("");
				t2.setText("");
				t3.setText("");
				t4.setText("");
				t5.setText("");
				t6.setText("");
				t1.requestFocus();
			}
		}
		if (e.getSource() == b3) {
			if (e.getKeyCode() == e.VK_ENTER)
				System.exit(0);
			if (e.getKeyCode() == e.VK_UP)
				t4.requestFocus();
			if (e.getKeyCode() == e.VK_LEFT)
				b2.requestFocus();
			if (e.getKeyCode() == e.VK_SPACE)
				System.exit(0);
		}
	}

	public void keyReleased(KeyEvent e) {
	}

	public void keyTyped(KeyEvent e) {
	}
}
